<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Rules for Modelling Code
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial083.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial079.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial085.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc155">11.5</A>&nbsp;&nbsp;Rules for Modelling Code</H2><UL>
<LI><A HREF="tutorial084.html#toc73">Disjunctions</A>
<LI><A HREF="tutorial084.html#toc74">Conditionals</A>
</UL>

In CLP, the declarative model is at the same time the constraint setup code.
This code should therefore be deterministic and terminating, so:
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>Careful with disjunctions</B><DD CLASS="dd-description">
 Don't leave choice-points (alternatives for backtracking).
 Choices should be deferred until search phase.
<DT CLASS="dt-description"><B>Use only simple conditionals</B><DD CLASS="dd-description">
 Conditions in <CODE>(...-&gt;...;...)</CODE> must be true or false at modelling time!
<DT CLASS="dt-description"><B>Use only structural recursion and loops</B><DD CLASS="dd-description">
 Termination conditions must be know at modelling time!
</DL>
<A NAME="toc73"></A>
<H3 CLASS="subsection"><A NAME="htoc156">11.5.1</A>&nbsp;&nbsp;Disjunctions</H3>
Disjunctions in the model should be avoided. Assume that a naive
model would contain the following disjunction:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
% DO NOT USE THIS IN A MODEL
no_overlap(S1,D1,S2,D2) :- S1 #&gt;= S2 + D2.
no_overlap(S1,D1,S2,D2) :- S2 #&gt;= S1 + D1.
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
There are two basic ways of treating the disjunction:
<UL CLASS="itemize"><LI CLASS="li-itemize">
Deferring the choice until the search phase by introducing a
 decision variable.
<LI CLASS="li-itemize">Changing the behaviour of the disjunction so it becomes a constraint
 (see also <A HREF="tutorial098.html#chapimpl">14</A> and <A HREF="tutorial107.html#chappropiachr">15</A>).
</UL>
In the example, we can introduce a boolean variable <CODE>B{0,1}</CODE> which represents
the choice.
The actual choice can be then be taken in search code by choosing a
value for the variable. The model code must then be changed to observe
the decision variable, either using the delay facility of ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP>:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
delay no_overlap(S1,D1,S2,D2,B) if var(B).
no_overlap(S1,D1,S2,D2,0) :- S1 #&gt;= S2 + D2.
no_overlap(S1,D1,S2,D2,1) :- S2 #&gt;= S1 + D1.
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
or using an arithmetic encoding like in

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
no_overlap(S1,D1,S2,D2,B) :-
        B :: 0..1, 
        S1 +     B*1000 #&gt;= S2 + D2,
        S2 + (1-B)*1000 #&gt;= S1 + D1.
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
The alternative of turning the disjunction into a proper constraint is
achieved most easily using <EM>propia</EM>'s infer-annotation
(see <A HREF="tutorial107.html#chappropiachr">15</A>). The original formulation of neighbour/2
is kept but it is used as follows:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
    ..., no_overlap(S1,D2,S2,D2) infers most, ...
</PRE></BLOCKQUOTE></TD>
</TR></TABLE><BR>
<A NAME="toc74"></A>
<H3 CLASS="subsection"><A NAME="htoc157">11.5.2</A>&nbsp;&nbsp;Conditionals</H3>
Similar considerations apply to conditionals where the condition is not
decidable at constraint setup time. For example, suppose we want to
impose a no-overlap constraint only if two tasks share the same resource.
The following code is currently not safe in ECLiPSe:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
nos(Res1, Res2, Start1, Dur1, Start2, Dur2) :-
    ( Res1 #= Res2 -&gt;           % WRONG!!!
        no_overlap(Start1, Dur1, Start2, Dur2)
    ;
        true
    )
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
The reason is that (at constraint setup time) Res1 and Res2 will most
likely be still uninstantiated. Therefore, the condition will in general
delay (rather than succeed or fail), but the conditional construct
will erroneously take this for a success and take the first alternative.<BR>
<BR>
Again, this can be handled using delay

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
delay nos(Res1, Res2, _, _, _, _) if nonground([Res1,Res2]).
nos(Res1, Res2, Start1, Dur1, Start2, Dur2) :-
    ( Res1 == Res2 -&gt;
        no_overlap(Start1, Dur1, Start2, Dur2)
    ;
        true
    ).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
It might also be possible to compute a boolean variable indicating the
truth of the condition. This is particularly easy when a reified
constraint can be used to express the condition, like in this case:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
nos(Res1, Res2, Start1, Dur1, Start2, Dur2) :-
    #=(Res1, Res2, Share),
    cond_no_overlap(Start1, Dur1, Start2, Dur2, Share).

delay cond_no_overlap(_,_,_,_,Share) if var(Share).
cond_no_overlap(Start1, Dur1, Start2, Dur2, Share) :-
    ( Share == 1 -&gt;
        no_overlap(Start1, Dur1, Start2, Dur2)
    ;
        true
    ).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE><BR>
<HR>
<A HREF="tutorial083.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial079.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial085.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
